/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.document.report.impl;

import java.util.ArrayList;
import java.util.List;

import rat.document.IDocumentCollection;
import rat.document.report.IDocumentReporter;
import rat.report.text.IPrinter;

/**
 * Provides a base implementation that stores input until report is requested.
 *
 */
public abstract class AbstractDocumentCollectionReporter implements IDocumentReporter {

    private final List namedDocumentCollections;
    private final List failures;
    
    protected AbstractDocumentCollectionReporter() {
        namedDocumentCollections = new ArrayList();
        failures = new ArrayList();
    }
    
    public final void reportOn(final String name, final IDocumentCollection documentCollection) {
        final NamedDocumentCollection namedCollection = new NamedDocumentCollection(name, documentCollection);
        namedDocumentCollections.add(namedCollection);
    }

    public final void reportOn(final String name, final Exception exception) {
        final DocumentCollectionLoadFailure failure = new DocumentCollectionLoadFailure(name, exception);
        failures.add(failure);
    }

    public final void report(final IPrinter printer) {
        final NamedDocumentCollection[] namedDocumentCollectionArray = 
            (NamedDocumentCollection[]) namedDocumentCollections.toArray(
                    NamedDocumentCollection.EMPTY_ARRAY); 
        final DocumentCollectionLoadFailure[] failuresArray = (DocumentCollectionLoadFailure[]) 
            failures.toArray(DocumentCollectionLoadFailure.EMPTY_ARRAY);
        report(printer, namedDocumentCollectionArray, failuresArray);
    }

    /**
     * Report to the given printer on the given named document collections.
     * @param printer <code>IPrinter</code>, not null
     * @param namedDocumentCollection <code>NamedDocumentCollection</code>'s, not null
     */
    protected abstract void report(IPrinter printer, NamedDocumentCollection[] namedDocumentCollection, 
            DocumentCollectionLoadFailure[] failures);
    
}
